package com.mathassistant.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.event.TopicPopupEditEvent;
import com.mathassistant.client.event.topic.TopicListUpdateEvent;
import com.mathassistant.client.helper.ClickPoint;
import com.mathassistant.client.helper.RPCCall;
import com.mathassistant.client.service.TopicServiceAsync;
import com.mathassistant.shared.TopicDTO;

public class TopicPopupPresenter implements Presenter {
	
	public interface Display{
		HasClickHandlers getEditButton();
	    HasClickHandlers getDeleteButton();
	    
	    HasText getFriendNameLabel(); //du
	    void hideDialog();
	    
	    void setTitle(String displayName); //du
	    void setTitleAndShow(String displayName, ClickPoint location);
		Widget asWidget();
	}
	
	private Display display;
	private SimpleEventBus eventBus;
	private TopicDTO topic;
	private TopicServiceAsync rpcService;
	public Display getDisplay()
	{
		return display;
	}
	public TopicPopupPresenter(TopicDTO topic, TopicServiceAsync rpcService,SimpleEventBus eventBus, Display view){
		this.topic = topic;
		this.rpcService = rpcService;
		this.display = view;
		this.eventBus = eventBus;
		bind();
	}
	private void bind() {
		// TODO Auto-generated method stub
		display.getEditButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new TopicPopupEditEvent(topic));
				display.hideDialog();
			}
		});
		
		display.getDeleteButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				doDelete();
				display.hideDialog();
			}
		});
		
	}
	protected void doDelete() {
		// TODO Auto-generated method stub
		final String area = topic.getArea();
		final String contentType = topic.getContentType();
		new RPCCall<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("Error fetching topics: " + caught.getMessage());
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new TopicListUpdateEvent(area,contentType));
			}

			@Override
			protected void callService(AsyncCallback<Void> cb) {
				// TODO Auto-generated method stub
				rpcService.deleteTopic(topic, cb);
			}
		}.retry(3);
		
	}
	@Override
	public void go(HasWidgets container) {
		// TODO Auto-generated method stub

	}

	@Override
	public void go() {
		// TODO Auto-generated method stub

	}

}
